﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SqlServer = DataCoreTechnologies.SqlServer;

namespace DataCoreTechnologies.Scribe.Business.Html
{
    internal class DataTypesDocument : Document
    {
        #region internal
        //Constructor
        internal DataTypesDocument(SqlServer.Database database)
        {
            DocumentType = DocumentType.DataTypes;
            RootDatabase = database;
            RootServer = RootDatabase.Server;
            URL = "index." + database.Name + "." + "datatypes.html";
            _database = database;
        }
        //Properties
        internal override string DisplayName { get { return "Data Types"; } }
        //Methods
        internal override string GetHtml()
        {
            if (_html == "")
            {
                AddCssLine(@"      	#mainBody *.schemaColumn { width: 15%; }");
                AddCssLine(@"      	#mainBody *.systemTypeColumn { width: 15%; }");
                AddCssLine(@"      	#mainBody *.lengthColumn { width: 10%; }");
                AddCssLine(@"      	#mainBody *.maxLengthColumn { width: 10%; }");
                AddCssLine(@"      	#mainBody *.precisionColumn { width: 10%; }");
                AddCssLine(@"      	#mainBody *.scaleColumn { width: 10%; }");
                AddCssLine(@"      	#mainBody *.nullableColumn { width: 10%; }");

                AddCssLine(@"      	#mainBody *.categoryColumn { width: 25%; }");

                AddBodyLine(@"<div id=""mainBody"">");
                Boolean evenRow = true;

                string heirarchy = @"<h2 id=""topNav"">Location: <a href=""http://index.html"">" + RootServer.Name +
                    @"</a> . ";
                heirarchy += @"<a href=""http://index." +
                    _database.Name + @".html"">" + ScrubHtml(_database.Name) + @"</a></h2>";
                AddBodyLine(heirarchy);
                AddBodyLine(@"<h2 id=""title"">" + ScrubHtml(_database.Name) + @" Data Types</h2>");

                #region User Defined Data Types
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_userTypes"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('userTypes');"">User Defined Data Types</a>");
                AddBodyLine(@"</h2>");
                
                AddBodyLine(@"<div id=""userTypes"">");
                AddBodyLine(@"<table>");
                string colGroup = @"<colgroup><col class=""nameColumn""><col class=""schemaColumn"">" +
                    @"<col class=""systemTypeColumn""><col class=""lengthColumn""><col class=""maxLengthColumn"">" +
                    @"<col class=""precisionColumn""><col class=""scaleColumn""><col class=""nullableColumn""></colgroup>";
                AddBodyLine(colGroup);
                AddBodyLine(@"<tr><th>Name</th><th>Schema</th><th>System Type</th><th>Length</th><th>Max Length</th><th>Precision</th><th>Scale</th><th>Nullable</th></tr>");
                
                var customTypes = (from schema in _database.Schemas
                                   from customType in schema.UserTypes
                                   orderby customType.Name, schema.Name
                                   select customType);

                evenRow = true;
                foreach (SqlServer.UserType customType in customTypes)
                {
                    StringBuilder sb = new StringBuilder();
                    
                    if (evenRow == true) 
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(customType.Name) + @"</td>");

                    string href = @"<td>";
                    href += @"<a href=""http://index." + RootDatabase.Name
                        + @".schemas.html"">" + ScrubHtml(customType.Schema.Name) + @"</a>";
                    href += @"</td>";
                    sb.Append(href);

                    sb.Append(@"<td>" + ScrubHtml(customType.SystemType.Name) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(AddCommasToNumericString(customType.MaxLength.ToString())) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(AddCommasToNumericString(ActualMaxLength(customType.MaxLength, customType.SystemType).ToString())) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(customType.Precision.ToString()) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(customType.Scale.ToString()) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(customType.IsNullable.ToString()) + @"</td></tr>");
                    AddBodyLine(sb.ToString());
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion
                #region System Defined Data Types
                AddBodyLine(@"<h2>");
                AddBodyLine(@"<img id=""img_systemTypes"" src=""" + RootFolderPath + @"minus.jpg""> ");
                AddBodyLine(@"<a href=""javascript:;"" onmousedown=""toggleDiv('systemTypes');"">System Defined Data Types</a>");
                AddBodyLine(@"</h2>");
                
                AddBodyLine(@"<div id=""systemTypes"">");
                AddBodyLine(@"<table>");
                colGroup = @"<colgroup><col class=""nameColumn""><col class=""categoryColumn"">" +
                    @"<col class=""lengthColumn""><col class=""maxLengthColumn"">" +
                    @"<col class=""precisionColumn""><col class=""scaleColumn""><col class=""nullableColumn""></colgroup>";
                AddBodyLine(colGroup);
                AddBodyLine(@"<tr><th>Name</th><th>Category</th><th>Length</th><th>Max Length</th><th>Precision</th><th>Scale</th><th>Nullable</th></tr>");

                var systemTypes = (from systemType in _database.SystemTypes
                                   orderby systemType.Name
                                   select systemType);

                evenRow = true;
                foreach (SqlServer.SystemType systemType in systemTypes)
                {
                    StringBuilder sb = new StringBuilder();

                    if (evenRow == true)
                        sb.Append(@"<tr class=""evenRow"">");
                    else
                        sb.Append(@"<tr class=""oddRow"">");
                    evenRow = !evenRow;

                    sb.Append(@"<td>" + ScrubHtml(systemType.Name) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(systemType.Category.ToString()) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(AddCommasToNumericString(systemType.MaxLength.ToString())) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(AddCommasToNumericString(ActualMaxLength(systemType.MaxLength, systemType).ToString())) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(systemType.Precision.ToString()) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(systemType.Scale.ToString()) + @"</td>");
                    sb.Append(@"<td>" + ScrubHtml(systemType.IsNullable.ToString()) + @"</td></tr>");
                    AddBodyLine(sb.ToString());
                }
                AddBodyLine(@"</table>");
                AddBodyLine(@"</div>");
                #endregion

                AddBodyLine(@"</div>");
                _html = CreateHtmlDocument();
            }
            return _html;
        }
        #endregion
        #region Private
        private SqlServer.Database _database;
        #endregion
    }
}
